﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows;
using System.Collections.Specialized;
using DirectoryViewModels;
using System.Threading;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC.FolderExplorer
{
    public partial class FolderViewModel : BaseNotifyPropertyChangedUI
    {
        public event EventHandler ClearSubfolders;
        public event EventHandler SubfoldersPopulated;
        public static List<FolderViewModel> AllFoldersInMemory;
        public static bool Changed;

        public FolderViewModel(DirectoriesToExamineViewModel vm, FolderViewModel parent)
        {
            _parent = parent;
            this.FullPath = @"c:\";
            _vm = vm;
            _depth = GetDepth();
        }

        public void AddOperation(string name)
        {
            _FolderOperations.AddOperation(new FolderOperation(name), this);
        }

        public void RemoveOperation(string name)
        {
            _FolderOperations.RemoveOperation(name, this);
        }

        protected void ClearSubFolders()
        {
            OnClearSubfolders();
            if (_subFoldersForUIBinding != null)
            {
                _subFoldersForUIBinding.ForEach(f => f.ClearSubFolders());
                _subFoldersForUIBinding.Clear();
            }
            _subFoldersForUIBinding = null;
            Changed = true;
        }

        public void RefreshSubfolders()
        {
            ClearSubFolders();
            Files.Clear();
            _files = null;
            RaiseRefreshSubFolders();
        }

        private void RaiseRefreshSubFolders()
        {
            OnPropertyChanged("SubFolders");
            OnPropertyChanged("Files");
            OnPropertyChanged("DirectorySizeLong");
            OnPropertyChanged("DirectorySize");
            OnPropertyChanged("DirectorySizeNonZero");
            OnPropertyChanged("CurrentFolder");
        }

        protected void OnClearSubfolders()
        {
            ClearSubfolders.RaiseEvent(this);
        }

        private int GetDepth()
        {
            if (_parent == null)
                return 0;

            return 1 + _parent.GetDepth();
        }

        public void ClearFiles()
        {            
            CrawlInMemoryFolders(f => f.ClearFiles());
            this._files = null;
            // OnPropertyChanged("Files");
        }

        public void RaiseCurrentWorkChanged()
        {
            RaisePropertyChangedUIThread("CurrentWork");
        }

        public List<FolderViewModel> GetAllFoldersInMemory()
        {
            if (!Changed && AllFoldersInMemory != null)
            {
                return AllFoldersInMemory;
            }
            else
            {
                List<FolderViewModel> results = new List<FolderViewModel>();
                FolderViewModel f = FindRoot();
                f.CrawlInMemoryFolders(null, results);
                Changed = false;
                AllFoldersInMemory = results;
                return results;
            }            
        }

        public List<FileViewModel> GetAllFilesInMemory()
        {
            List<FileViewModel> results = new List<FileViewModel>();            
            FolderViewModel f = FindRoot();
            f.CrawlInMemoryFolders(folder =>
            {
                if (folder.FilesCached != null)
                {
                    results.AddRange(folder.FilesCached);
                }
            });
            return results;
        }

        protected void OnSubfoldersPopulated()
        {
            Changed = true;
            SubfoldersPopulated.RaiseEvent(this);            
        }
    }
}
